//
// Created by qinf on 2025/7/27.
//

#include "udp.h"
#include "parseData.h"
#include <iostream>
#include <chrono>

int main() {
    std::cout << "Server started..." << std::endl;

    //组播
    // 监听组播地址端口，注意第三个参数 true 表示启用组播
    ServerUDP server(10003, (char*)"227.0.0.1", true);


    //单播
//    server.setip_add(6075, (char*)"192.168.1.51");//改为自己的ip地址
//    server.setip_add(10003, (char*)"127.0.0.1");//改为自己的ip地址
    //127.0.0.1为本机ip地址
    //10001 任务分配数据, 10002航点数据,10003 侦察目标数据

    char buffer[2048];
    int count_all = 0;
    auto start = std::chrono::system_clock::now();
    while (true) {
        int len = server.receiveData(buffer, sizeof(buffer));
        std::cout << "Received " << len << " bytes" << std::endl;
        // 将 buffer 转为 vector<uint8_t>
        std::vector<uint8_t> recvData(buffer, buffer + len);


        // 调用解析函数
        parseData(recvData);
        //检测速度
        auto now = std::chrono::system_clock::now();
        count_all++;
        std::cout << "count_all : " << count_all << std::endl;
        auto durtion = now - start;
        auto mill = std::chrono::duration_cast<std::chrono::milliseconds>(durtion);
        std::cout << "Time cost : " << mill.count() << "ms" << std::endl;
    }
    return 0;
}

